<!-- 阶段分布 -->
<template>
  <div id="highchartsWordCloud" v-loading="loading" style="height: 250px; width: 100%;" />
</template>

<script>
import * as echarts from 'echarts'
import 'echarts-wordcloud'
import { getCiYunEcharts } from '@/api/bf/indirectProjectAnalysis/indirectProjectSummary'

export default {
  data() {
    return {
      chart: null,
      loading: false,
      chartData: [],
      // 当前选中的图表块
      currentChart: undefined
    }
  },
  watch: {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.getChartsData()
  },
  methods: {
    getChartsData() {
      this.loading = true
      getCiYunEcharts().then(response => {
        this.loading = false
        this.chartData = response.data
        this.initChart()
      })
    },
    initChart() {
      var _this = this
      this.chart = echarts.init(this.$el)
      window.onresize = function() {
        this.chart.resize()
      }
      var option = {
        tooltip: { show: true },
        series: [{
          type: 'wordCloud',
          sizeRange: [12, 30],
          rotationRange: [-20, 20],
          shape: 'circle',
          textPadding: 0,
          width: 'auto',
          height: 'auto',
          autoSize: { enable: true, minSize: 6 },
          textStyle: {
            normal: {
              color: () => {
                return ('rgb(' + [Math.round(Math.random() * 256), Math.round(Math.random() * 256), Math.round(Math.random() * 256)].join(',') + ')')
              }
            },
            emphasis: { shadowBlur: 2, shadowColor: '#333' }
          },
          data: this.chartData
        }]
      }
      this.chart.setOption(option, true)
      this.chart.on('click', function(params) {
        // 控制台打印数据的名称
        if (_this.currentChart === params.data.label) {
          _this.currentChart = 'notNull'
        } else {
          _this.currentChart = params.data.label
        }
        _this.$emit('changeLabel', _this.currentChart)
      })
    }
  }
}
</script>

